package com.syf.config.result;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @desc 平台通用返回结果
 *
 * @author syf
 */
@JsonInclude(value= JsonInclude.Include.NON_NULL)  //在序列化操作时 序列化不为null的属性(用在类上)
@Builder  //生成一个全属性的构造器(用来生成对象)，并且可以为对象链式赋值(lombok)
@NoArgsConstructor
@AllArgsConstructor
@Data
public class ResponseResult<T> implements Result {

	private static final long serialVersionUID = 874200365941306385L;

	private Integer code;

	private String msg;

	private T data;


	public ResponseResult(Integer code , String msg){
		this.code = code;
		this.msg = msg;
	}


	public static ResponseResult success() {
		ResponseResult result = new ResponseResult();
		result.setResultCode(RequestStatusEnum.REQUESR_SUCCESS);
		return result;
	}

	public static ResponseResult success(Object data) {
		ResponseResult result = new ResponseResult();
		result.setResultCode(RequestStatusEnum.REQUESR_SUCCESS);
		result.setData(data);
		return result;
	}

	public static ResponseResult failure(ResultCodeEnum resultCode) {
		ResponseResult result = new ResponseResult();
		result.setResultCode(resultCode);
		return result;
	}

	public static ResponseResult failure(ResultCodeEnum resultCode, Object data) {
		ResponseResult result = new ResponseResult();
		result.setResultCode(resultCode);
		result.setData(data);
		return result;
	}

	public static ResponseResult failure(String message) {
		ResponseResult result = new ResponseResult();
		result.setCode(RequestStatusEnum.REQUEEST_ERROR.getCode());
		result.setMsg(message);
		return result;
	}

	private void setResultCode(ResultCodeEnum code) {
		this.code = code.getCode();
		this.msg = code.getMsg();
	}

}
